Public without sharing class PriceAdjustmentHandler {
    public static void upsertPrice(List<PriceAdjustment__c> list_new,Map<Id,PriceAdjustment__c>oldMap  ){
        
        //保存价格体系调整Id
        Set<Id> paIds = new Set<Id>();
        for(PriceAdjustment__c pa: list_new){
            PriceAdjustment__c old = oldMap.get(pa.Id);
            if(pa.Status__c == '已批准' && old.Status__c != pa.Status__c){
            
                upsertPrice(pa);
            }
        
        }
    
    
    }

    private static void upsertPrice(PriceAdjustment__c pa  ){

            Id  groupType = Utils.getRecordTypeIdByDevNameAndObjName('PriceAdjustmentHead', 'PriceAdjustment__c');
             Id  allType  = Utils.getRecordTypeIdByDevNameAndObjName('AdjustAll', 'PriceAdjustment__c');

            ///价格调整明细
            Map<String,PriceAdjustmentDetail__c> proCom = new Map<String, PriceAdjustmentDetail__c>();
            List<PriceAdjustmentDetail__c> pads = [select Product__c,PriceAdjustment__c,NewInvoiceDealerPrice__c,
             NewAreaDealerPrice__c, NewMarketPrice__c, AdjustmentType__c,
             NewHospitalBottomPrice__c,

            NewHospitalManagerPrice__c ,NewMarketPriceUSD__c,NewSalesManagerPrice__c,NewSalesDirectorPrice__c,
            NewSpecialPrice__c,NewProvincialAgencyPrice__c 
            from PriceAdjustmentDetail__c where PriceAdjustment__c =: pa.Id ];
           
            //集团统一调价
            if(pa.RecordTypeId  == allType){

               if(pa.CompanyDup__c == null )pa.addError('价格体系适用公司不能为空');
                List<String> companys = pa.CompanyDup__c.split(';'); 
                Set<String>provinces = new Set<String>();
                Map<String,List<String>> comProvinceMap = new Map<String,List<String>> ();

                for(String company : companys){
                    List<String> ps = CompanyProvinceUtil.getCompanyNameAndProvince(company);
                    provinces.addAll(ps);
                    comProvinceMap.put(company, ps);
                }

                 Set<Id>pids = new Set<Id>();
                Set<Id>sealPids = new Set<Id>();
                for (PriceAdjustmentDetail__c pad : pads){


                    if(pad.AdjustmentType__c == '封存'){
                        sealPids.add(pad.Product__c );
                        continue;
                    }

                    pids.add(pad.Product__c);
                     
                
                }
                sealPrice(sealPids,pa);
                if(pids.isEmpty())return;

                Map<String  ,Price__c> priceMap  = new Map<String ,Price__c>();

                for (Price__c p : [select Product__c,Province__c,Company__c,CompanyBottomPrice__c,HospitalBottomPrice__c,Taxrate__c,Subsidiary_price__c,
                           InvoiceDealerPrice__c,MarketPriceUSD__c,AreaDealerPrice__c,MarketPrice__c,HospitalManagerPrice__c,InstCost__c,RepairCost__c,
                           SalesmanPrice__c,SalesDirectorPrice__c,SalesManagerPrice__c,SpecialPrice__c,ProvincialAgencyPrice__c,
                           GroupTerminalGuidePrice__c,GroupDeclarationDealerPrice__c,GroupRegionalDistributorsPrice__c 
                           from Price__c where Product__c in: pids and Company__c in:companys  and Province__c in:provinces]){
                    
                    priceMap.put(p.Product__c+p.Company__c+p.Province__c,p); 

                }

                List<Price__c> pL = new List<Price__c>();
                for (PriceAdjustmentDetail__c pad : pads){
                    if(sealPids.contains(pad.Product__c ) )continue;
                    for (String company  : comProvinceMap.keySet()) {
                        
                        for (String province  :  comProvinceMap.get(company )) {
                            String key= pad.Product__c + company + province;
                            Price__c pr = setPriceValue(pad,pa,priceMap.get(key),true  );
                            pr.Province__c = province;
                            pr.Company__c = company;
                            pL.add(pr );
                        }
                    }
                  
                
                }
                if(!pL.isEmpty())upsert pL;


                //调集团价

            }else if(pa.RecordTypeId == groupType ){

                 List<Product__c> pLi = new  List<Product__c> ();
                for (PriceAdjustmentDetail__c pad : pads){
                    
                   
                    Product__c p = new Product__c();
                    p.Id = pad.Product__c;
                    p.GroupRegionalDistributorsPrice__c = pad.NewAreaDealerPrice__c;
                    p.GroupMarketPrice__c = pad.NewMarketPrice__c;
                    p.GroupTerminalGuidePric__c = pad.NewHospitalManagerPrice__c;
                    p.GroupProvincialAgencyPrice__c  = pad.NewProvincialAgencyPrice__c;
                    p.GroupSpecialPrice__c = pad.NewSpecialPrice__c;
                    p.GroupDeclarationDealerPrice__c  = pad.NewInvoiceDealerPrice__c;
                    pLi.add(p);
                
                }
                if(!pLi.isEmpty())update pLi;
                //调子公司价
            }else{

                Id  subType = Utils.getRecordTypeIdByDevNameAndObjName('PriceAdjustmentSub', 'PriceAdjustment__c');

                Set<Id>pids = new Set<Id>();
                Set<Id>sealPids = new Set<Id>();
                for (PriceAdjustmentDetail__c pad : pads){


                    if(pad.AdjustmentType__c == '封存'){
                        sealPids.add(pad.Product__c );
                        continue;
                    }

                    pids.add(pad.Product__c);
                     
                
                }
                sealPrice(sealPids,pa);
                if(pids.isEmpty())return;

                 Map<Id ,Price__c> priceMap  = new Map<Id,Price__c>();
                for (Price__c p : [select Id,Product__c,Company__c,CompanyBottomPrice__c,HospitalBottomPrice__c,Taxrate__c,Subsidiary_price__c,
                           InvoiceDealerPrice__c,MarketPriceUSD__c,AreaDealerPrice__c,MarketPrice__c,HospitalManagerPrice__c,InstCost__c,RepairCost__c,
                           SalesmanPrice__c,SalesDirectorPrice__c,SalesManagerPrice__c,SpecialPrice__c,ProvincialAgencyPrice__c,
                           GroupTerminalGuidePrice__c,GroupDeclarationDealerPrice__c,GroupRegionalDistributorsPrice__c 
                           from Price__c where Product__c in: pids and Company__c =:pa.Company__c  and Province__c=:pa.Province__c]){
                    
                    priceMap.put(p.Product__c,p); 

                }

                List<Price__c> pL = new List<Price__c>();
                for (PriceAdjustmentDetail__c pad : pads){
                    if(sealPids.contains(pad.Product__c ) )continue;


                    pL.add(setPriceValue(pad,pa,priceMap.get(pad.Product__c),pa.RecordTypeId == subType  ));
                
                }
                if(!pL.isEmpty())upsert pL;

            }
           
    }
    /**
        封存价格
    */
    public static void  sealPrice(Set<Id> pids ,PriceAdjustment__c pa  ){
        if(pids.isEmpty())return;
        List<Price__c> pList =new List<Price__c>();
        for(Price__c p : [select Id from Price__c where Product__c in:pids   and Company__c =:pa.Company__c  and Province__c=:pa.Province__c]){
            p.ClosePrice__c = true;
            pList.add(p);
        }
        if(!pList.isEmpty())update pList;
        if(!pList.isEmpty())Approval.lock(pList,false);

    }
    private static Price__c setPriceValue(PriceAdjustmentDetail__c pad,PriceAdjustment__c pa  ,Price__c pr ,Boolean isSub  ){

        if(pr == null) pr = new Price__c();
        pr.Product__c = pad.Product__c;
        pr.Company__c = pa.Company__c;
        pr.Province__c = pa.Province__c;
        pr.ClosePrice__c = false;

        if(isSub ){
            pr.InvoiceDealerPrice__c = pad.NewInvoiceDealerPrice__c;
            pr.AreaDealerPrice__c = pad.NewAreaDealerPrice__c;
            pr.MarketPrice__c = pad.NewMarketPrice__c;
            pr.HospitalManagerPrice__c = pad.NewHospitalManagerPrice__c;
            pr.ProvincialAgencyPrice__c = pad.NewProvincialAgencyPrice__c;
            pr.SpecialPrice__c = pad.NewSpecialPrice__c;

        }else{
            pr.MarketPriceUSD__c = pad.NewMarketPriceUSD__c;
            pr.SalesmanPrice__c = pad.NewHospitalBottomPrice__c;
            pr.SalesManagerPrice__c = pad.NewSalesManagerPrice__c;
            pr.SalesDirectorPrice__c = pad.NewSalesDirectorPrice__c;
        }

        return pr;


    }


}